<script setup>
const setInput = (dom, value) => {
    let inputLabel = dom; //这里获取需要自动录入的input内容
    let lastValue = inputLabel.value;
    inputLabel.value = value;
    let event = new Event("input", { bubbles: true });
    //  React15
    event.simulated = true;
    //  React16 内部定义了descriptor拦截value，此处重置状态
    let tracker = inputLabel._valueTracker;
    if (tracker) {
        tracker.setValue(lastValue);
    }
    inputLabel.dispatchEvent(event);
}

const eventInit = {
    view: window,  // 要确保‘window’是可用的，并且指向正确的窗口上下文
    bubbles: true,
    cancelable: true
};

const handle = () => {
    if (window.location.pathname === '/') {
        let loginBtn = document.querySelector('button[type="button"].btn-home.btn-login')
        if (loginBtn) {
            // 创建一个点击事件
            // const clickEvent = new MouseEvent('click', eventInit);
            // 触发点击事件
            // loginLink.dispatchEvent(clickEvent);
            loginBtn.click()
        }
    }
    if (window.location.pathname === '/login') {
        let emailInput = document.querySelector('input[placeholder="Email"]')
        if (emailInput) {
            setInput(emailInput, 'rafa_im@163.com')
        }
        let passowrdInput = document.querySelector('input[type="password"]')
        if (passowrdInput) {
            setInput(passowrdInput, 'Lirifa1992')
        }
        let loginBtn = document.querySelector('.login-form .login-form-button')
        setTimeout(() => {
            loginBtn.click()
        }, 100)

    }
}

onMounted(() => {
    setTimeout(() => {
        handle()
        window.addEventListener('popstate', () => {
            handle()
        });
    }, 100)
})
</script>